<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><span style="font-size: 20px">首页</span></el-breadcrumb-item>
        <el-breadcrumb-item><span style="font-size: 20px">用户管理</span></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"
                v-model="username"></el-input><!--此处的username需要后端传递-->
      <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
      <h1></h1>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="headleAdd">新增 <i class="el-icon-circle-plus-outline"></i> </el-button>
      <h1></h1>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="codeName" label="工号" width="200">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="150">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope"><!--此处的slot-scope已被废弃,但暂没找到替代-->
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
            <!--注意这里在后台传了个id-->
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0"><!--分页查询-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"><!--分页查询数据绑定-->
        <!--此处的pageNum和pageSize由前端传递,total由后台返回给前端-->
        <!--total记得从后端传一下-->
        <!--在页面中pageNum是翻页,pageSize是左边的 *条每页,total是左边的 共*条-->
      </el-pagination>
    </div>

    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="120">
        <el-form-item label="工号">
          <el-input v-model="form.codeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select v-model="form.status" placeholder="请选择身份">
            <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.value"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账号">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="密码">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
	import {user_page,user_delete,user_update} from '../axios/api/userApi.js'
export default {
  name: "User",
  data(){

    return{
      tableData:[],
      total:0,
      pageNum:1,
      pageSize:10,
      //上方为分页模块中的变量
      userid:"",
      //username为搜索框的变量,记得绑定
      username:"",
      form:{},
      dialogFormVisible:false,
      multipleSelection: [],
      headerBg:'headerBg',
      options: [{
        id: '1',
        value: '学生'
      }, {
        id: '2',
        value: '教师'
      }, {
        id: '3',
        value: '管理员'
      }],
	  status: undefined
    }
  },
  created() {
      this.load()
  },
  methods:{
    //连接数据库,我在其他需要连接的地方统一调用load()
    //记得改你自己的地址,我的vue的默认端口是8080
    //我此处的连接用了axios，在src/utils文件中的request.js，若不想使用或报错，建议删了我的用自己的方法
    //我把地址放在了request.js开头部分的baseURL中了，原本url为http://localhost:9090/user/page?pageNum=
    load(){
      // this.request.get("/user/page?pageNum=",{
      //   params:{
      //     pageNum:  this.pageNum,
      //     pageSize: this.pageSize,
      //     username: this.username
      //   }
      // }).then(res => {
      //   console.log(res)
      // })
      // //如果报错request is not defined，则开头的request.get()改成this.request.get()

      // this.tableData = res.records
      // //tableData就是那个表格
      // //此处的records为表格中需要的数值，此处我是把表中的数据放入了records,记得改成你自己的

      // this.total = res.total
      //total是页面中左下角的 共*条
	  const data = {
		  pageNum: this.pageNum,
		  pageSize: this.pageSize
	  }
		user_page(data).then(res=>{
			console.log(res.data)
			const r = res.data
			this.tableData = r.records
			this.total = r.total
		})

    },

    //新增功能中的确认按钮中的方法，用于传输数据与判断
    //在后台中可以添加 creat_time 使其倒序，就能把新增的数据第一个展示
    //此处的url与上面的load()同理
    save(){
	  this.form.status = this.status ? this.status : this.form.status
      user_update(this.form).then(res=>{
		  this.status = undefined
		  this.dialogFormVisible = false
		  this.dialogFormVisible = false
		  this.form = {}
		  this.load()
	  })
    },

    //新增功能,编辑和新增可以用同一个接口
    headleAdd(){
      this.dialogFormVisible = true //打开弹窗
      this.form = {}
    },

    //编辑功能,row是被传入的值,编辑和新增可以用同一个接口,注意后台改一下
    //this.form = Object.assign({},row),将row拷贝到空对象中，可以解决没点确定但数据已经改变的问题
    handleEdit(row){
      this.form = row  //将编辑的值赋予
      this.dialogFormVisible = true //打开弹窗
    },

    //删除功能,这里后台传了个id,地址处理与上面用了相同的手法
    del(id){
      user_delete(id).then(res=>{
		  this.load()
	  })
    },

    //多选按钮
    handleSelectionChange(val){
      console.log(val)
      this.multipleSelection = val
    },

    //为pageSize与pageNum设置连接
    handleSizeChange(pageSize){
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    }
  }
}
</script>

<style>
.headerBg{
  background: #eee!important;
}
</style>